using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace MoEngine
{
    public class MasterWindowCollection : WindowCollection<MasterWindow>
    {
    }

    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class WindowsManager : CoreComponentDrawable
    {
        protected MasterWindowCollection m_MasterWindows = new MasterWindowCollection();

        public WindowsManager()
        {
            // TODO: Construct any child components here
        }
                                
        public override void Initialize()
        {
            base.Initialize();

            // hook up input
            Core.InputMgr.MouseDown += new MouseButtonEvent(OnMouseDown);
            Core.InputMgr.MouseUp += new MouseButtonEvent(OnMouseUp);
            Core.InputMgr.MouseMove += new MouseMoveEvent(OnMouseMove);

            // init master windows
            foreach (MasterWindow window in m_MasterWindows)
            {
                window.Initialize();
            }

            // after init, further added windows will need to be init manually
            // TODO: note here remove and readd migth cause problem (init more than once...)
            m_MasterWindows.WindowAdded += new EventHandler<WindowEventArgs>(OnMasterWindowAdded);
        }

        void OnMouseMove(Point mousePos, InputEventArgs evtArgs)
        {
            m_MasterWindows.HandleMouseInput(MouseEventType.MouseMove, MouseButtons.Left, mousePos, evtArgs);
        }

        void OnMouseUp(MouseButtons button, Point mousePos, InputEventArgs evtArgs)
        {
            m_MasterWindows.HandleMouseInput(MouseEventType.MouseUp, button, mousePos, evtArgs);
        }

        void OnMouseDown(MouseButtons button, Point mousePos, InputEventArgs evtArgs)
        {
            m_MasterWindows.HandleMouseInput(MouseEventType.MouseDown, button, mousePos, evtArgs);
        }

        void OnMasterWindowAdded(object sender, WindowEventArgs e)
        {
            e.Window.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime appTime)
        {
            base.Update(appTime);
            // TODO: Add your update code here
            // update all master windows
            m_MasterWindows.UpdateAll(appTime);
        }

        public override void Draw(GameTime appTime)
        {
            base.Draw(appTime);
            // draw all master windows
            m_MasterWindows.DrawAll(appTime);
        }

        public MasterWindowCollection MasterWindows
        {
            get { return m_MasterWindows; }
        }
    }
}